/**
 *
 */
package com.wedroid.r2d2.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wedroid.entity.contact.Contacter;
import com.wedroid.entity.contact.ContacterType;
import com.wedroid.entity.contact.Droid;
import com.wedroid.entity.contact.GroupMember;
import com.wedroid.r2d2.dao.ContacterDaoSupport;
import com.wedroid.r2d2.dao.DroidDaoSupport;
import com.wedroid.r2d2.dao.GroupMemberDaoSupport;
import com.wedroid.r2d2.service.ContactService;

/**
 * @author lute
 */
@Service("contactService")
public class ContactServiceImpl implements ContactService {

    @Autowired
    private DroidDaoSupport droidDaoSupport;

    @Autowired
    private ContacterDaoSupport contacterDaoSupport;

    @Autowired
    private GroupMemberDaoSupport groupMemberDaoSupport;

    /* (non-Javadoc)
     * @see com.wedroid.r2d2.service.ContactService#createDroid(com.wedroid.entity.contact.Droid)
     */
    public void createDroid(Droid droid) {
        droidDaoSupport.createDroid(droid);
    }

    /* (non-Javadoc)
     * @see com.wedroid.r2d2.service.ContactService#getDroid(java.lang.String)
     */
    public Droid getDroid(String name) {
        return droidDaoSupport.getDroidByName(name);
    }

    /* (non-Javadoc)
     * @see com.wedroid.r2d2.service.ContactService#createOrUpdateContacter(com.wedroid.entity.contact.Contacter)
     */
    public Contacter createOrUpdateContacter(Contacter contacter) {
        Contacter existingContacter = contacterDaoSupport.getContacter(contacter.getDroid().getId(), contacter.getProfile().getNickName());
        if (existingContacter == null) {
            Long id = contacterDaoSupport.createContacter(contacter);
            contacter.setId(id);
        } else {
            contacter.setId(existingContacter.getId());

            // Use old head image for the moment
            contacter.headImgUrl(existingContacter.getProfile().getHeadImgUrl());

            contacterDaoSupport.updateContacter(contacter);
        }
        return contacter;
    }

    /* (non-Javadoc)
     * @see com.wedroid.r2d2.service.ContactService#updateContacter(com.wedroid.entity.contact.Contacter)
     */
    public void updateContacter(Contacter contacter) {
        contacterDaoSupport.updateContacter(contacter);
    }

    /* (non-Javadoc)
     * @see com.wedroid.r2d2.service.ContactService#createOrUpdateGroupMember(com.wedroid.entity.contact.GroupMember)
     */
    public void createOrUpdateGroupMember(GroupMember groupMember) {
        Long droidId = groupMember.getDroid().getId();
        Long groupId = groupMember.getGroup().getId();
        GroupMember existingGroupMember = groupMemberDaoSupport.getGroupMember(droidId, groupId, groupMember.getProfile().getNickName());
        if (existingGroupMember == null) {
            groupMemberDaoSupport.createGroupMember(groupMember);
        } else {
            groupMember.setId(existingGroupMember.getId());

            // Use old head image for the moment
            groupMember.headImgUrl(existingGroupMember.getProfile().getHeadImgUrl());

            groupMemberDaoSupport.updateGroupMemberProfile(groupMember);
        }
    }

    /* (non-Javadoc)
     * @see com.wedroid.r2d2.service.ContactService#getGroupMember(com.wedroid.entity.contact.Contacter, java.lang.String)
     */
    public GroupMember getGroupMember(Contacter group, String name) {
        if (group != null && group.getContacterTypeEnum().equals(ContacterType.GROUP)) {
            Droid droid = group.getDroid();
            return groupMemberDaoSupport.getGroupMember(droid.getId(), group.getId(), name);
        }
        return null;
    }

    /* (non-Javadoc)
     * @see com.wedroid.r2d2.service.ContactService#updateGroupMember(com.wedroid.entity.contact.GroupMember)
     */
    public void updateGroupMember(GroupMember groupMember) {
        groupMemberDaoSupport.updateGroupMemberProfile(groupMember);
    }

}
